package cz.muni.fi.pa165.dao.impl;

import cz.muni.fi.pa165.dao.CustomerDAO;
import cz.muni.fi.pa165.entity.Customer;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

/**
 * @author <a href="mailto:vramik at redhat.com">Vlastislav Ramik</a>
 */
@Stateless
public class CustomerDAOImpl implements CustomerDAO {
    
    //@Inject
    @PersistenceContext(unitName = "customer-unit", type = PersistenceContextType.TRANSACTION)
    private EntityManager em;

    @Override
    public void createCustomer(Customer customer) {
        checkCustomerAttributes(customer);
        if (customer.getId() != null) {
            throw new IllegalArgumentException("Customer's id has to be null but it's already set");
        }
        em.persist(customer);
    }

    @Override
    public void removeCustomer(Customer customer) {
        if (customer == null) {
            throw new IllegalArgumentException("Customer is null.");
        }
        if (customer.getId() == null) {
            throw new IllegalArgumentException("Customer's id is null.");
        }
//        em.remove(em.merge(customer));
        em.remove(customer);
    }

    @Override
    public void updateCustomer(Customer customer) {
        checkCustomerAttributes(customer);
        if (customer.getId() == null) {
            throw new IllegalArgumentException("Customer's id is null.");
        }
        em.merge(customer);
    }

    @Override
    public List<Customer> getAllCustomers() {
        return em.createQuery("select c from Customer c ORDER BY c.name ASC").getResultList();
    }

    @Override
    public Customer getCustomerById(Long id) {
//        if (id == null) {
//            throw new IllegalArgumentException("Id is null.");
//        }
        return em.find(Customer.class, id);
    }

    private void checkCustomerAttributes(Customer customer) {
        if (customer == null) {
            throw new IllegalArgumentException("Customer is null");
        }
        if (customer.getName() == null || customer.getName().isEmpty()) {
            throw new IllegalArgumentException("Customer's name is null or empty.");
        }
        if (customer.getAddress() == null || customer.getAddress().isEmpty()) {
            throw new IllegalArgumentException("Customer's address is null or empty.");
        }
        if (customer.getZip() == null || customer.getZip().isEmpty()) {
            throw new IllegalArgumentException("Customer's ZIP is null or empty.");
        }
        if (customer.getCity() == null || customer.getCity().isEmpty()) {
            throw new IllegalArgumentException("Customer's city is null or empty.");
        }
        if (customer.getCountry() == null || customer.getCountry().isEmpty()) {
            throw new IllegalArgumentException("Customer's name is null or empty.");
        }
        if (customer.getAccountNumber() == null || customer.getAccountNumber().isEmpty()) {
            throw new IllegalArgumentException("Customer's name is null or empty.");
        }
    }
}
